package com.test.system.admin.shiro;

import com.test.system.admin.dao.MenuDao;
import com.test.system.admin.dao.PermDao;
import com.test.system.admin.pojo.Menu;
import org.apache.shiro.config.Ini;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author 徒有琴
 */
public class MyShiroFilterFactoryBean extends ShiroFilterFactoryBean {
    private MenuDao menuDao;
    private PermDao permDao;
    private String filterChainDefinitions;

    public void setMenuDao(MenuDao menuDao) {
        this.menuDao = menuDao;
    }

    public void setPermDao(PermDao permDao) {
        this.permDao = permDao;
    }

    @Override
    public void setFilterChainDefinitions(String definitions) {
        filterChainDefinitions=definitions;
        Ini ini = new Ini();
        ini.load(definitions);
        Ini.Section section = ini.getSection("urls");
        if (CollectionUtils.isEmpty(section)) {
            section = ini.getSection("");
        }
        //加入动态规则,先查出所有的菜单
        List<Menu> menus = menuDao.getMenuList();
        for (Menu menu : menus) {
            if (StringUtils.hasText(menu.getUrl())) {//如果url有值
                //查询出能访问当前菜单的id  roles[1,3,5,6]
                List<Integer> roleIds=permDao.getMenuRoleIds(menu.getId());
                section.put(menu.getUrl(), "roles"+roleIds);
            }
        }
        section.put("/**","authc"); //所有的上面没有包含的地址，都必须登陆后才能访问
        this.setFilterChainDefinitionMap(section);
    }

    public synchronized void update() {
        try {
            AbstractShiroFilter shiroFilter = (AbstractShiroFilter) this.getObject();
            PathMatchingFilterChainResolver resolver = (PathMatchingFilterChainResolver) shiroFilter
                    .getFilterChainResolver();
            // 过滤管理器
            DefaultFilterChainManager manager = (DefaultFilterChainManager) resolver.getFilterChainManager();
            // 清除权限配置
            manager.getFilterChains().clear();
            this.getFilterChainDefinitionMap().clear();

            // 重新设置权限
            this.setFilterChainDefinitions(filterChainDefinitions);


            Map<String, String> chains = this.getFilterChainDefinitionMap();
            if (!CollectionUtils.isEmpty(chains)) {
                Iterator var12 = chains.entrySet().iterator();

                while (var12.hasNext()) {
                    Map.Entry<String, String> entry = (Map.Entry) var12.next();
                    String url = entry.getKey();
                    String chainDefinition = entry.getValue();
                    manager.createChain(url, chainDefinition);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
